<!--
 * @Descripttion: 
 * @version: 
 * @Author: ybac
 * @Date: 2023-03-02 14:11:16
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-06-07 18:44:20
-->
<template>
  <a-modal title="请选择租户" :width="450" :visible="visible" :closable="false" :maskClosable="false">
    <a-form-model ref="ruleForm" :model="params" :rules="rules" :labelCol="{ span: 3 }" :wrapperCol="{ span: 20 }">
      <a-form-model-item label="租户：" style="margin-bottom: 10px" prop="selectIndex">
        <a-select v-model="params.selectIndex" placeholder="请选择登录租户">
          <a-select-option v-for="(item, index) in rentList" :key="index" :value="index">
            {{ item.tenantInfo.tenantName }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
    <template slot="footer">
      <a-button type="primary" @click="selectOk">确认</a-button>
    </template>
  </a-modal>
</template>

<script>
import Vue from 'vue'
import { getAction } from '@/api/manage'
import { USER_INFO, TENANT_ID } from '@/store/mutation-types'
import { apiBaseUrl } from '@/utils/request'
import axios from 'axios'
import { postAction } from '@/api/manage'

export default {
  name: 'LoginSelectTenant',
  data() {
    return {
      visible: false,
      rentList: [],
      params: {
        selectIndex: undefined,
      },
      rules: {
        selectIndex: [{ required: true, message: '请选择租户', trigger: 'change' }],
      },
      loginParams: {},
      userId: '',
    }
  },

  methods: {
    show(loginfo) {
      this.loginParams = {...loginfo}
      postAction('/sys/getTenantInfoByPhone', { phone: loginfo.username }).then((res) => {
        if (res.result && res.result.length > 0) {
          if (res.result.length === 1) {
            this.userId = res.result[0].user.id
            this.phoneLogin()
          } else {
            this.rentList = res.result
            this.visible = true
          }
        } else {
          this.$emit('fail', res)
        }
      })
    },
    selectOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let user = this.rentList[this.params.selectIndex].user
          this.visible = false
          this.userId = user.id
          this.phoneLogin()
        } else {
          return false
        }
      })
    },
    phoneLogin() {
      postAction('/sys/phoneLogin', {...this.loginParams,userId:this.userId})
        .then((res) => {
          if (res.code == 200) {
            this.$emit('success', res.result,)
          } else {
            this.$emit('fail', res)
          }
        })
        .catch((err) => {
          this.$emit('fail', err)
        })
    },
    loginAfter(loginParams) {
      Vue.ls.set(TENANT_ID, loginParams.tenantId, 7 * 24 * 60 * 60 * 1000)
      this.$store.commit('SET_TENANT', loginParams.tenantId)
      axios({
        method: 'post',
        headers: {
          'Content-Type': 'application/json;charset=UTF-8',
        },
        url: apiBaseUrl + '/jksaas-mgt/mgt/sys/login',
        data: loginParams,
      })
        .then((res) => {
          if (res.data.code == 200) {
            this.$emit('success', res.data.result)
          } else {
            this.$emit('fail', res.data)
          }
        })
        .catch((err) => {
          this.$emit('fail', err)
        })
    },
  },
}
</script>

<style scoped></style>
